package com.example.testandroid;

import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{

	private Context context;
	private List<String> item_names;
	private List<Integer> item_imgs;
	
	
	public MyAdapter(Context context,List<String> item_names,List<Integer> item_imgs) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.item_names = item_names;
		this.item_imgs = item_imgs;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return item_names.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//总布局
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.CENTER);
		//名字
		TextView tv_item = new TextView(context);
		tv_item.setGravity(Gravity.CENTER);
		tv_item.setLayoutParams(new GridView.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_item.setText(item_names.get(position));
		//图片
		ImageView img_item = new ImageView(context);
		img_item.setLayoutParams(new LayoutParams(50, 50));
		img_item.setImageResource(item_imgs.get(position));
		
		layout.addView(img_item);
		layout.addView(tv_item);
		return layout;
	}

}
